const blogTypeSer = require('../../service/blog/blogTypeSer')
const sqlCondition = require('../../utils/sqlCondition')

class BlogCon {
    // 查询数据
    async getBlogType(ctx) {
        const jsonData = sqlCondition(ctx.request.body.searchData)
        const listQuery = sqlCondition(ctx.request.body.listQuery)
        try {
            const data = await blogTypeSer.getBlogType(jsonData, listQuery)
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

    // 查询所有类型
    async getBlogTypeAll(ctx) {
        try {
            const data = await blogTypeSer.getBlogTypeAll()
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

    // 添加
    async addBlogType(ctx) {
        const jsonData = sqlCondition(ctx.request.body)
        if (!jsonData) {
            ctx.body = {
                code: '999',
                message: '博客类型名称不允许为空'
            }
            return
        }
        try {
            const data = await blogTypeSer.addBlogType(jsonData)
            if (data.message) {
                ctx.body = {
                    code: '999',
                    data: data
                }
                return
            }
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    // 修改
    async editBlogType(ctx) {
        const jsonData = ctx.request.body
        try {
            const data = await blogTypeSer.editBlogType(jsonData)
            if (data.message) {
                ctx.body = {
                    code: '999',
                    data: data
                }
                return
            }
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            console.log(e)
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

    // 删除
    async deleteBlogType(ctx) {
        const jsonData = sqlCondition(ctx.request.body)
        if (!jsonData) {
            ctx.body = {
                code: '999',
                message: '博客类型名称不允许为空'
            }
            return
        }

        try {
            const data = await blogTypeSer.deleteBlogType(jsonData)
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

}

module.exports = new BlogCon()
